package exp4;
import  exp3.userdata;
import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet("/register11")
public class Register extends HttpServlet {
    private static final EntityManagerFactory entityManagerFactory;
    private static final String PERSISTENCE_UNIT_NAME = "MyJPADemoPU";
    java.sql.Connection conn = null;
    java.sql.Statement sqlStmt = null; //语句对象
    ResultSet sqlRst = null; //结果集对象

    static {
        entityManagerFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
    }

    private EntityManager getEntityManager() {
        return entityManagerFactory.createEntityManager();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            InitialContext context = new InitialContext();
            DataSource dataSource =
                    (DataSource) context.lookup("jpa/DSTest");
            EntityManager em = getEntityManager();
            EntityTransaction tx = em.getTransaction();
            HttpSession session = request.getSession(true);
            String id = request.getParameter("name");
            String name = request.getParameter("user");
            String email = request.getParameter("mail");
            String password = request.getParameter("password");
            String job=request.getParameter("job");
            conn = dataSource.getConnection();
            sqlStmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

//执行Sql语句
            String  sqlQuery = "select id,email from userdata";
            System.out.println(id+name+email+password+job);
            sqlRst = sqlStmt.executeQuery(sqlQuery);
            int flag=0;
            while (sqlRst.next()) {
                if(sqlRst.getString("id").equals(id)){
                    flag=1;
                }
    }
            if(flag==1)response.sendRedirect("/error.html");
            userdata UserDTO = new userdata(id,name , password,email,job);
            try {
                tx.begin();
                em.persist(UserDTO);
                tx.commit();
            } catch (Exception ex) {
                throw new ServletException(ex);
            } finally {
                em.close();
            }
            request.getSession().setAttribute("user", UserDTO);
            response.sendRedirect("http://localhost:8080/success.jsp");
        } catch (NamingException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);

    }

}
